package com.yanqu.road.dao.impl.activity.seacraft;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.seacraft.SeacraftSouthSeaCrossChallengeData;
import com.yanqu.road.utils.property.PropertyHelper;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description
 * @Author cwq
 * @Data 2022/2/14 14:22
 */
public class SeacraftSouthSeaCrossChallengeDataDaoImpl extends TempDataObjectDao<SeacraftSouthSeaCrossChallengeData> {

    public static String getSeacraftSouthSeaCrossChallengeDataSql = "select * from `t_c_activity_seacraft_southsea_challenge` where `activityId`=? ";
    public Map<Long,SeacraftSouthSeaCrossChallengeData> getSeacraftSouthSeaCrossChallengeDataMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long,SeacraftSouthSeaCrossChallengeData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getSeacraftSouthSeaCrossChallengeDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                SeacraftSouthSeaCrossChallengeData item = parse(rs);
                map.put(item.getUserId(),item);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateSeacraftSouthSeaCrossChallengeDataSql = "update `t_c_activity_seacraft_southsea_challenge` set targetId=?,attackType=?,teamType=?,patronsInfoList=?,winStreakNum=?,receiveWinStreakTimes=?,buyBuffId=?,attackedList=?,randomMap=?,buffValueList=?,lockDurable=?,deductDurable=?,totalScore=?,distance=?,targetDistance=?,killPatronsNum=?,targetUnionRank=?,winStreakReward=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(SeacraftSouthSeaCrossChallengeData seacraftSouthSeaCrossChallengeData) {
        seacraftSouthSeaCrossChallengeData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, seacraftSouthSeaCrossChallengeData.getTargetId()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossChallengeData.getAttackType()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossChallengeData.getTeamType()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftSouthSeaCrossChallengeData.getPatronsInfoList())));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossChallengeData.getWinStreakNum()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossChallengeData.getReceiveWinStreakTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossChallengeData.getBuyBuffId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftSouthSeaCrossChallengeData.getAttackedList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftSouthSeaCrossChallengeData.getRandomMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftSouthSeaCrossChallengeData.getBuffValueList())));
        para.put(i++, new DbParameter(Types.BIGINT, seacraftSouthSeaCrossChallengeData.getLockDurable()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossChallengeData.getDeductDurable()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossChallengeData.getTotalScore()));
        para.put(i++, new DbParameter(Types.BIGINT, seacraftSouthSeaCrossChallengeData.getDistance()));
        para.put(i++, new DbParameter(Types.BIGINT, seacraftSouthSeaCrossChallengeData.getTargetDistance()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossChallengeData.getKillPatronsNum()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossChallengeData.getTargetUnionRank()));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(seacraftSouthSeaCrossChallengeData.getWinStreakReward())));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossChallengeData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, seacraftSouthSeaCrossChallengeData.getUserId()));
        if (getDb().executeNoneQuery(updateSeacraftSouthSeaCrossChallengeDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSeacraftSouthSeaCrossChallengeDataSql = "insert into `t_c_activity_seacraft_southsea_challenge`(activityId,userId,targetId,attackType,teamType,patronsInfoList,winStreakNum,receiveWinStreakTimes,buyBuffId,attackedList,randomMap,buffValueList,lockDurable,deductDurable,totalScore,distance,targetDistance,killPatronsNum,targetUnionRank,winStreakReward) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(SeacraftSouthSeaCrossChallengeData seacraftSouthSeaCrossChallengeData) {
        seacraftSouthSeaCrossChallengeData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossChallengeData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, seacraftSouthSeaCrossChallengeData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, seacraftSouthSeaCrossChallengeData.getTargetId()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossChallengeData.getAttackType()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossChallengeData.getTeamType()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftSouthSeaCrossChallengeData.getPatronsInfoList())));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossChallengeData.getWinStreakNum()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossChallengeData.getReceiveWinStreakTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossChallengeData.getBuyBuffId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftSouthSeaCrossChallengeData.getAttackedList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftSouthSeaCrossChallengeData.getRandomMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftSouthSeaCrossChallengeData.getBuffValueList())));
        para.put(i++, new DbParameter(Types.BIGINT, seacraftSouthSeaCrossChallengeData.getLockDurable()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossChallengeData.getDeductDurable()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossChallengeData.getTotalScore()));
        para.put(i++, new DbParameter(Types.BIGINT, seacraftSouthSeaCrossChallengeData.getDistance()));
        para.put(i++, new DbParameter(Types.BIGINT, seacraftSouthSeaCrossChallengeData.getTargetDistance()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossChallengeData.getKillPatronsNum()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossChallengeData.getTargetUnionRank()));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(seacraftSouthSeaCrossChallengeData.getWinStreakReward())));
        if (getDb().executeNoneQuery(insertSeacraftSouthSeaCrossChallengeDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected SeacraftSouthSeaCrossChallengeData parseData(ResultSet rs) throws SQLException {
        SeacraftSouthSeaCrossChallengeData seacraftSouthSeaCrossChallengeData = new SeacraftSouthSeaCrossChallengeData();
        seacraftSouthSeaCrossChallengeData.setActivityId(rs.getInt("activityId"));
        seacraftSouthSeaCrossChallengeData.setUserId(rs.getLong("userId"));
        seacraftSouthSeaCrossChallengeData.setTargetId(rs.getString("targetId"));
        seacraftSouthSeaCrossChallengeData.setAttackType(rs.getInt("attackType"));
        seacraftSouthSeaCrossChallengeData.setTeamType(rs.getInt("teamType"));
        seacraftSouthSeaCrossChallengeData.setPatronsInfoList(JSON.parseObject(rs.getString("patronsInfoList"), new TypeReference<java.util.List<com.yanqu.road.entity.activity.seacraft.SeacraftSouthSeaLockPatronsInfo>>() {}));
        seacraftSouthSeaCrossChallengeData.setWinStreakNum(rs.getInt("winStreakNum"));
        seacraftSouthSeaCrossChallengeData.setReceiveWinStreakTimes(rs.getInt("receiveWinStreakTimes"));
        seacraftSouthSeaCrossChallengeData.setBuyBuffId(rs.getInt("buyBuffId"));
        seacraftSouthSeaCrossChallengeData.setAttackedList(JSON.parseObject(rs.getString("attackedList"), new TypeReference<java.util.List<java.lang.Long>>() {}));
        seacraftSouthSeaCrossChallengeData.setRandomMap(JSON.parseObject(rs.getString("randomMap"), new TypeReference<java.util.Map<java.lang.Long, java.lang.Integer>>() {}));
        seacraftSouthSeaCrossChallengeData.setBuffValueList(JSON.parseObject(rs.getString("buffValueList"), new TypeReference<java.util.List<java.lang.Integer>>() {}));
        seacraftSouthSeaCrossChallengeData.setLockDurable(rs.getLong("lockDurable"));
        seacraftSouthSeaCrossChallengeData.setDeductDurable(rs.getInt("deductDurable"));
        seacraftSouthSeaCrossChallengeData.setTotalScore(rs.getInt("totalScore"));
        seacraftSouthSeaCrossChallengeData.setDistance(rs.getLong("distance"));
        seacraftSouthSeaCrossChallengeData.setTargetDistance(rs.getLong("targetDistance"));
        seacraftSouthSeaCrossChallengeData.setKillPatronsNum(rs.getInt("killPatronsNum"));
        seacraftSouthSeaCrossChallengeData.setTargetUnionRank(rs.getInt("targetUnionRank"));
        seacraftSouthSeaCrossChallengeData.setWinStreakReward(PropertyHelper.parseStringToProperty(rs.getString("winStreakReward")));

        return seacraftSouthSeaCrossChallengeData;
    }

}
